#ifndef __COMMON_H__
#define __COMMON_H__

#include <pthread.h>

/******************************************************************************
宏定义区
*******************************************************************************/

// 常用的数值区域 
#define HALF_UNIT_SIZE 8
#define UNIT_SIZE    	16
#define TWO_UNIT_SIZE		32
#define FOUR_UNIT_SIZE		64
#define EIGHT_UNIT_SIZE		128
#define QUART_K_SIZE		256
#define HALF_K_SIZE 		512
#define K_SIZE			1024
#define TWO_K_SIZE		2048
#define FOUR_K_SIZE		4096
#define EIGHT_K_SIZE		8192
#define M_SIZE			1024000

#define CMD_ARG_NUM UNIT_SIZE
#define MAX_PIPE_NUM	TWO_UNIT_SIZE
#define MAX_PIPE_FD_NUM	MAX_PIPE_NUM*2

#define MAX_THREADS_NUM			32
// 返回码
#define RC_SUCCESS			0
#define RC_FAILURE			-1
#define RC_NOSPACE			-100
#define RC_NOTFOUND			-200
#define RC_NULL_POINTER			-300

//命令
#define CMD_PUT			"put"



/******************************************************************************
结构体定义区
*******************************************************************************/

typedef struct cmd
{
	char m_scmdname[TWO_UNIT_SIZE];
	int  m_icmdtype;
	int m_inum;
	char m_scmdarg[8][TWO_UNIT_SIZE];
}cmd;


typedef struct spidercfg
{
	int m_imaxworker;
	char m_siniturl[HALF_K_SIZE];
	char m_suseragent[HALF_K_SIZE];
	char m_sfilearchivedir[HALF_K_SIZE];
}spidercfg;


typedef struct urlinfo
{
	char m_surl[HALF_K_SIZE];
}urlinfo;


typedef struct cvector_elem
{
	void* m_pval;
}cvector_elem;


typedef struct cvector
{
	cvector_elem *m_pbase;
	int m_itotalnum;
	int m_icur;
}cvecotr;


typedef struct node
{
	void* m_pvalue;
	node* m_pnext;
}node;

typedef struct nodearray
{
	node *m_pbase;
	int m_itotalnum;
	int m_iarraysize;
}nodearray;


typedef cvector_elem queue_elem;

typedef struct queue
{
	queue_elem *m_pbase;
	int m_itotalnum;
	int m_icur;
	int m_iactualused;
}queue;


typedef struct event
{
	int m_ifd;
	void (*m_func)(int ifd, int event, void* arg);
	int m_ievent;
	void *m_parg;
}event;

typedef struct destinfo
{
	char m_sip[16];
	int m_iport;
	char m_sdns[HALF_K_SIZE];
}destinfo;

typedef struct destinfo httpinfo;


typedef struct reqinfo
{
	int m_icur;
	char m_sreqtime[TWO_UNIT_SIZE];
	char m_surl[HALF_K_SIZE];
}reqinfo;

typedef struct resinfo
{
	int m_iencode;
	int m_ibodylens;
}resinfo;

//common runtime info , the memory will be malloced in the initial time,
//every thread will get own info and avoid mallocing memory in rutime;
typedef struct runtimeinfo
{
	int *m_imainpfd;  //main pipe fd
	int *m_pepfd;
	nodearray *m_purlcontainer;  //用于存放所有的url
	nodearray *m_pnarr[MAX_THREADS_NUM]; // 用于存放event, 每个线程一个
	queue *m_pq[MAX_THREADS_NUM];  //用于存放url
	int *m_ppfd;	
	pthread_t *m_ptid;
	char *m_preq[MAX_THREADS_NUM]; // 用于发送消息的缓冲区
	char *m_pres[MAX_THREADS_NUM]; // 用于存放接收消息的缓冲区
	int *m_pressize;
}runtimeinfo;

extern httpinfo g_ohttpinfo;


int icheck_url(char* purl);

#endif
